# # from django.http import HttpResponse
# # from django.http import JsonResponse
# # from django.shortcuts import render
# #
# # # Create your views here.
# # from django.views import View
# # from rest_framework.generics import GenericAPIView
# # from rest_framework.views import APIView
# # from rest_framework.response import Response
# #
# # from booktest.models import BookInfo, HeroInfo
# # from booktest.serializers import BookInfoSerializer, HeroInfoSerializer
# #
# #
# # class BookInfoViewSet(GenericAPIView):
# #     queryset = BookInfo.objects.all()
# #     serializer_class = BookInfoSerializer
# #
# #     def get(self, request):
# #         qs = self.get_queryset()
# #         serializer = self.get_serializer(qs, many=True)
# #
# #         return Response(serializer.data)
# #
# #
# # def serialize(request):
# #     # book = BookInfo.objects.get(pk=1)
# #     # book = BookInfo.objects.all()
# #     # 当对多条数据序列化，必须加many=true
# #     # s = BookInfoSerializer(book,many=True)
# #     hero = HeroInfo.objects.get(id=2)
# #
# #     print(hero)
# #     return JsonResponse(hero)
# #     # return JsonResponse(hero.data),safe=False)
# # # 反序列化需要data=data
# # def deserizlize(request):
# #     data = {'btitle':'英语','bpub_date':'2000-01-01'}
# #     s= BookInfoSerializer(data=data)
# #     # raise_exception=True  出现错误报错
# #
# #     succuess = s.is_valid(raise_exception=True)
# #     print(succuess)
# #     # 打印错误信息
# #     print(s.errors)
# #     # 获取校验结束的数据
# #     print(s.validated_data)
# #     return JsonResponse(s.validated_data)
# #
# # class BookListView(APIView):
# #
# #     #获取全部图书
# #     def get(self,request):
# #         #1查询数据库
# #         books = BookInfo.objects.all()
# #         #2.初始化序列化起
# #         s = BookInfoSerializer(books,many=True)
# #         #3 返回json
# #         return Response(s.data)
# #
# # class BookDetailView(GenericAPIView):
# #     # 查询结果集
# #     queryset = BookInfo.objects.all()
# #     # 序列化器
# #     serializer_class = BookInfoSerializer
# #     # 如果有很多序列化器，需要用方法返回
# #     def get_serializer_class(self):
# #         return BookInfoSerializer
# #     def get(self,request,pk):
# #         book = self.get_object()
# #         s = self.get_serializer(book)
# #         return Response(s.data)
# # class BookListView(GenericAPIView):
# #     queryset = BookInfo.objects.all()
# #     serializer_class = BookInfoSerializer
# #     def get(self,request):
# #         books = self.get_queryset() #获取全部数据
# #         s = self.get_serializer(books,many=True)
# #         return Response(s.data)
# #
# #
# #
# from django.http import HttpResponse
# from django.http import JsonResponse
# from rest_framework.response import Response
#
# from booktest.models import BookInfo, HeroInfo
# from booktest.serializers import BookInfoSerializer, HeroInfoSerializer
#
# # 序列化
# # def search_one(request):
#     # hero = HeroInfo.objects.get(hname='郭靖')
#     # s = HeroInfoSerializer(hero)
#     # print(s.data)
#     # book = BookInfo.objects.get(btitle='射雕英雄传')
#     '''反序列化练习'''
#     '''反序列化练习1 继承modol 验证数据'''
#     # data = {"btitle":'django'}
#     # book = BookInfoSerializer(data=data,partial=True)
#     # success = book.is_valid()
#     # print(success)
#     # print(book.errors)
#     # return HttpResponse('ok')
#     '''--------------反序列化练习2  验证-创建对象进入数据库---------------------'''
#     # data = {'hname':"刘备",
#     #         'hbook':2,}
#     # s = HeroInfoSerializer(data=data,partial=True)
#     #
#     # success =# from django.http import HttpResponse
# from django.http import JsonResponse
# from django.shortcuts import render
#
# # Create your views here.
# from django.views import View
# from rest_framework.generics import GenericAPIView
# from rest_framework.views import APIView
# from rest_framework.response import Response
#
# from booktest.models import BookInfo, HeroInfo
# from booktest.serializers import BookInfoSerializer, HeroInfoSerializer
#
#
# class BookInfoViewSet(GenericAPIView):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#
#     def get(self, request):
#         qs = self.get_queryset()
#         serializer = self.get_serializer(qs, many=True)
#
#         return Response(serializer.data)
#
#
# def serialize(request):
#     # book = BookInfo.objects.get(pk=1)
#     # book = BookInfo.objects.all()
#     # 当对多条数据序列化，必须加many=true
#     # s = BookInfoSerializer(book,many=True)
#     hero = HeroInfo.objects.get(id=2)
#
#     print(hero)
#     return JsonResponse(hero)
#     # return JsonResponse(hero.data),safe=False)
# # 反序列化需要data=data
# def deserizlize(request):
#     data = {'btitle':'英语','bpub_date':'2000-01-01'}
#     s= BookInfoSerializer(data=data)
#     # raise_exception=True  出现错误报错
#
#     succuess = s.is_valid(raise_exception=True)
#     print(succuess)
#     # 打印错误信息
#     print(s.errors)
#     # 获取校验结束的数据
#     print(s.validated_data)
#     return JsonResponse(s.validated_data)
#
# class BookListView(APIView):
#
#     #获取全部图书
#     def get(self,request):
#         #1查询数据库
#         books = BookInfo.objects.all()
#         #2.初始化序列化起
#         s = BookInfoSerializer(books,many=True)
#         #3 返回json
#         return Response(s.data)
#
# class BookDetailView(GenericAPIView):
#     # 查询结果集
#     queryset = BookInfo.objects.all()
#     # 序列化器
#     serializer_class = BookInfoSerializer
#     # 如果有很多序列化器，需要用方法返回
#     def get_serializer_class(self):
#         return BookInfoSerializer
#     def get(self,request,pk):
#         book = self.get_object()
#         s = self.get_serializer(book)
#         return Response(s.data)
# class BookListView(GenericAPIView):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#     def get(self,request):
#         books = self.get_queryset() #获取全部数据
#         s = self.get_serializer(books,many=True)
#         return Response(s.data)
#
#
#
# from django.http import HttpResponse
# from django.http import JsonResponse
# from rest_framework.response import Response
#
# from booktest.models import BookInfo, HeroInfo
# from booktest.serializers import BookInfoSerializer, HeroInfoSerializer

# 序列化
from django.http import HttpResponse
from rest_framework.decorators import action
from rest_framework.generics import GenericAPIView, ListAPIView, ListCreateAPIView, CreateAPIView, RetrieveAPIView, \
    DestroyAPIView, UpdateAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, \
    DestroyModelMixin
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet, GenericViewSet, ModelViewSet, ReadOnlyModelViewSet

from booktest.models import BookInfo
from booktest.serializers import BookInfoSerializer
# def search_one(request):
# hero = HeroInfo.objects.get(hname='郭靖')
# s = HeroInfoSerializer(hero)
# print(s.data)
# book = BookInfo.objects.get(btitle='射雕英雄传')
# 反序列化练习1 继承modol 验证数据'''
# data = {"btitle":'django'}
# book = BookInfoSerializer(data=data,partial=True)
# success = book.is_valid()
# print(success)
# print(book.errors)
# return HttpResponse('ok')
# --------------反序列化练习2  验证-创建对象进入数据库---------------------'''
# data = {'hname':"刘备",
#         'hbook':2,}
# s = HeroInfoSerializer(data=data,partial=True)
#
# success = s.is_valid()
# print(success)
# print(s.errors)
# inst = s.save()
#
# return JsonResponse(s.data)

# '''-------------------------------------'''
# 序列化
# book = BookInfo.objects.get(pk=1)
# s = BookInfoSerializer(book)
# print(s.data)
# return HttpResponse(s.data)
# return JsonResponse(s.data)
#  s.is_valid()
#     # print(success)
#     # print(s.errors)
#     # inst = s.save()
#     #
#     # return JsonResponse(s.data)
#
#     '''-------------------------------------'''
#     # 序列化
#     # book = BookInfo.objects.get(pk=1)
#     # s = BookInfoSerializer(book)
#     # print(s.data)
#     # return HttpResponse(s.data)
#     # return JsonResponse(s.data)
#
from rest_framework.views import APIView

# class BookListView(APIView):
#     # 获取全部图书
#     def get(self, request):
#         # 1.获取全部图书
#         books = BookInfo.objects.all()
#         # 2/初始化序列化器
#         s = BookInfoSerializer(books, many=True)
#         # 3.返回json
#         return Response(s.data)


# class BookDetailView(GenericAPIView):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#
#     def get(self, request, pk):
#         book = self.get_object()
#         s = self.get_serializer(book)
#         return Response(s.data)
# class BookListView(GenericAPIView):
#     queryset = BookInfo.objects.all()
#     # serializer_class = BookInfoSerializer
#     # 获取多个数据
#     def get(self,request):
#         books = self.get_queryset()
#         s = self.get_serializer(books,many=True)
#         return Response(s.data)
#     def get_serializer_class(self):
#         return BookInfoSerializer
# class BookListView(ListModelMixin,CreateModelMixin,GenericAPIView):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#     def get(self,request):
#         return self.list(request)
#     def post(self,request):
#         # s = self.get_serializer(data=request.data)
#         # s.is_valid()
#         # s.save()
#         return self.create(request)
#
# class BookDetailView(RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#     def get(self,request,pk):
#         return  self.retrieve(request,pk)
#     def put(self,request,pk):
#         return self.update(request)
#     def delete(self,request,pk):
#         return self.destroy(request,pk)

# class BookListView(ListAPIView):
#      queryset = BookInfo.objects.all()
#      serializer_class = BookInfoSerializer
# class BookListView(ListCreateAPIView):
#      queryset = BookInfo.objects.all()
#      serializer_class = BookInfoSerializer
# class BookListView(CreateAPIView):
#      queryset = BookInfo.objects.all()
#      serializer_class = BookInfoSerializer



# class BookDetailView(RetrieveAPIView):
#
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer

# 视图集，5个动作list/create/retrieve单一数据/update/destroy
# 视图集配置陆游，as_view可以穿参数，来指定请求方法是哪个动作
# class BookInfoViewSet(ViewSet):
#     def list(self,request):
#         pass
#     def create(self,request):
#         pass
#     def retrieve(self,request,pk):
#         pass
#     def update(self,request,pk):
#         pass
#     def destroy(self,request,pk):
#         pass
# class BookInfoViewSet(ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin,GenericViewSet):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer


# class BookInfoViewSet(ModelViewSet):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer


'''
    视图apiview--GenericAPIView
    视图拓展类5个  CreateModelMixin......
    相关APIView子类9个继承了GenericAPIView和拓展类，对增删改查进行组合ListCreateAPIView...
    视图集5个，也可以使用拓展类   目的：多个路由对应一个视图，ViewSet---GenericViewSet
    视图提供两个子类帮我们继承拓展类ReadOnlyViewset/ModelViewSet，
    视图集可以自定义动作
    路由和视图集搭配使用
    每种方式练习删改差，增1增多
'''


# class BookDetailView(APIView):
#
#
#     def get(self, request):
#         # 获取全部
#         books = BookInfo.objects.all()
#         s = BookInfoSerializer(books, many=True)
#         return Response(s.data)
#
#
#     def get(self, request, pk):
#         # 获取单个
#         book = BookInfo.objects.get(id=pk)
#         s = BookInfoSerializer(book)
#         return Response(s.data)
#
#
#     def get(self, request):
#         # 创建一个对象()
#         date = {'btitle': "高数", 'bput_date': "2018-01-01"}
#         s = BookInfoSerializer(data=date)
#         s.is_valid(raise_exception=True)
#         s.save()
#         return Response(s.validated_data)
#
#
#     def get(self, request):
#         # 更新一个对象
#         data = {"btitle": "巴西柔术"}
#         book = BookInfo.objects.get(btitle='高数')
#         s = BookInfoSerializer(book, data=data, partial=True)
#         s.is_valid(raise_exception=True)
#         s.save()
#         return Response(s.validated_data)


# -------------------以下为GenericAPIView---------------------------
# class BookDetailView(GenericAPIView):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#
#     def get(self, request):
#         # 获取全部数据
#         books = self.get_queryset()
#         s = self.get_serializer(books, many=True)
#         return Response(s.data)
#
#     def get(self, request, pk):
#         # 获取单一数据
#         book = self.get_object()
#         s = self.get_serializer(book)
#         return Response(s.data)
#
#     def get(self, request):
#         # 增加一条数据
#         data = {'btitle': '泰拳', 'bput_date': '2018-01-01'}
#         s = self.get_serializer(data=data)
#         s.is_valid(raise_exception=True)
#         s.save()
#         return Response(s.validated_data)
#
#     def get(self, request):
#         data = {'btitle': '桑巴'}
#         book = BookInfo.objects.get(btitle='泰拳')
#         s = self.get_serializer(book, data=data, partial=True)
#         s.is_valid(raise_exception=True)
#         s.save()
#         return Response(s.validated_data)
# ---------------------以下为GenericAPIView的5个拓展类---------------------------


# class BookDetailView(CreateModelMixin,ListModelMixin,GenericAPIView):
#     # GenericAPIView拓展类查询列表和创建
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#     def get(self,request):
#         return self.list(request)
#     def post(self,request):
#         return self.create(request)
#
class BookDetailView(DestroyModelMixin, UpdateModelMixin, RetrieveModelMixin, GenericAPIView):
    # GenericAPIView拓展类单个对象和修改数据（？？？？？？？partial总是false）,删除
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer

    def get(self, request, pk):
        return self.retrieve(request)

    def post(self, request, pk):
        return self.update(request, partial=True)

    def put(self, request, pk):
        return self.destroy(request)
# -----------------------以下为GenericAPIView和5个拓展类的9个组合

# class BookDetailView(CreateAPIView):
#     # GenericAPIView拓展类查询列表和创建
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#
#
# class BookDetailView(ListAPIView):
#     # GenericAPIView拓展类查询列表和创建
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#
#
# class BookDetailView(RetrieveAPIView):
#     # GenericAPIView拓展类查询列表和创建
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#
#
# class BookDetailView(DestroyAPIView):
#     # GenericAPIView拓展类查询列表和创建
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer


# ----------??????有问题----------------
# class BookDetailView(UpdateAPIView):
# GenericAPIView拓展类查询列表和创建
# queryset = BookInfo.objects.all()
# serializer_class = BookInfoSerializer
#############-----------视图结果集ViewSet-------#############
# class BookDetailView(ViewSet):
#     def list(self, request):
#         return HttpResponse('list')
#
#     def retrieve(self, request, pk):
#         return HttpResponse('retrieve')
#
#     def create(self, request):
#         return HttpResponse('create')
#
#     def update(self, request, pk):
#         return HttpResponse('update')
#
#     def destory(self, request, pk):
#         return HttpResponse('destory')


# class BookDetailView(CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, ListModelMixin,
#                      GenericViewSet):
#     # GenericViewSet配合拓展类
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer


# class BookDetailView(ReadOnlyModelViewSet):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#
#
class BookInfoViewSet(ModelViewSet):
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer

    @action(methods=['get'], detail=False)
    def latest(self, request):
        book = BookInfo.objects.latest('id')
        s = self.get_serializer(book)
        return Response(s.data)
# class StandardPageNumberPagination(PageNumberPagination):
#     page_size_query_param = 'page_size'
#     max_page_size = 1
#     page_size=1
# class BookInfoView(ListAPIView):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#     pagination_class = StandardPageNumberPagination






